package com.pl.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pl.entity.Device;
import com.pl.service.DeviceService;
import com.pl.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Device)表控制层
 *
 * @author makejava
 * @since 2024-04-30 15:25:02
 */
@RestController
@RequestMapping("/api/device")
public class DeviceController {
    /**
     * 服务对象
     */
    @Autowired
    private DeviceService deviceService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String, Object> map) {
        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);

        //按照条件查询
        List<Device> list = deviceService.queryByPage(map);

        //返回 R 内部有 pageinfo
        if(list!=null && list.size()>0){  //返回成功数据
            PageInfo<Device> pageInfo = new PageInfo<>(list);  //目的是得到总行数 总页数
            return R.ok(pageInfo);
        }
        return R.fail("查询数据失败！");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.deviceService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param device 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(Device device) {
        boolean result = this.deviceService.insert(device);
        if(result){
        return R.ok();
        }
        return R.fail("添加失败！");
    }

    /**
     * 编辑数据
     *
     * @param device 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Device device) {
        boolean result= this.deviceService.update(device);
        if (result) {
            return R.ok();
        }
        return R.fail("修改失败！");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable int id) {
        return R.ok(this.deviceService.deleteById(id));
    }
    //批量删除
    @RequestMapping("/del/batch")
    public R deleteBatch(@RequestParam List<Integer>list) {

        boolean result = deviceService.deleteBatch(list);
        if (result) {
            return R.ok();
        }
        return R.fail("批量删除失败");

    }
    /**
     * 导入文件
     *
     * @param file
     * @return
     */
    @RequestMapping("/importDeviceFile")
    public String importProductFile(@Param("file") MultipartFile file) {
        return deviceService.importSysUserFile(file);
    }

    /**
     * 导出文件
     *
     * @param response
     */
    @RequestMapping("/exportDeviceFile")
    public void exportDeviceFile(HttpServletResponse response) {
       deviceService.exportDeviceFile(response);
    }

    /**
     * 查询全部数据
     */
    @RequestMapping("/finAll")
    public R finAll() {
        boolean result = deviceService.finAll();
        if (result) {
            return R.ok();
        }
        return R.fail("查询失败");
    }
}

